package com.edu.ujn.myh.action;

import java.util.List;
import java.util.Random;

import org.apache.struts2.ServletActionContext;

import com.edu.ujn.myh.domain.ProfessorInfo;
import com.edu.ujn.myh.domain.RoleInfo;
import com.edu.ujn.myh.domain.UserInfo;
import com.edu.ujn.myh.service.IApplyService;
import com.edu.ujn.myh.service.ILoginService;
import com.edu.ujn.myh.utils.Encrypt;
import com.edu.ujn.myh.utils.SendMail;

/**
 * 
 * @作者 马永华
 * @日期 2010-4-13下午09:04:23
 * @版本 1.0.0 登录action
 */
public class LoginAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7209636419193962979L;
	private static String userName ="";
	private String passWord;
	private String mail = "";
	private String state;
	private ILoginService loginService = null;
	private IApplyService applyService = null;
	private String newPassWord;
	private int roleId;
	private int[] checkbox;
	// 用户注册
	public String register() {
		state = "success";
		if (userName == null || "".equals(userName)) {
			state = "error";
			return SUCCESS;
		}
		if (passWord == null || "".equals(passWord)) {
			state = "error";
			return SUCCESS;
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName.trim());
		userInfo.setPassWord(Encrypt.MD5(passWord.trim()));
		userInfo.setMail(mail.trim());
		userInfo.setRoleId(1);
		String str = loginService.registerService(userInfo);
		if ("error".equals(str)) {
			state = "error";
		}

		return SUCCESS;
	}

	// 用户登录
	public String login() {
		String str = "success";
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName.trim());
		userInfo.setPassWord(Encrypt.MD5(passWord.trim()));
		List list = loginService.login(userInfo);
		if (list == null || list.size() == 0) {
			return "error";
		}
		ProfessorInfo pInfo = new ProfessorInfo();
		pInfo.setUserName(userName.trim());
		ProfessorInfo p = applyService.getProfessor(pInfo);
		if (p != null) {
			getRequest().getSession().setAttribute("pId", p.getId());
		}
		ServletActionContext.getRequest().getSession().setAttribute("roleList",
				list);
		ServletActionContext.getRequest().getSession().setAttribute("userName",
				userName);
		return str;

	}
//	获得左边的权限树
	public String getTree() {
		List roleList = (List) ServletActionContext.getRequest().getSession()
				.getAttribute("roleList");
		List privList = null;
		if (roleList != null && roleList.size() > 0) {
			privList = loginService.userPrivList(roleList);
		}
		ServletActionContext.getRequest().setAttribute("privList", privList);
		return SUCCESS;
	}
//	用户密码修改
	public String passModify(){
		String userName = (String)getRequest().getSession().getAttribute("userName");
		if(userName==null||"".equals(userName)){
			return "login";
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName.trim());
		userInfo.setPassWord(Encrypt.MD5(passWord));
		List list = loginService.login(userInfo);
		if (list == null || list.size() == 0) {
			return "error";
		}
		userInfo.setPassWord(Encrypt.MD5(newPassWord));
		String str = loginService.passModify(userInfo);
		if("error".equals(str)){
			return "error";
		}
		return SUCCESS;
	}
//	检查用户名是否存在
	public String checkUserName(){
		String str = loginService.checkUserName(userName);
		if("success".equals(str)){
			state = "success";
			return SUCCESS;
		}
		state = "error";
		return SUCCESS;
	}
	
//获得角色列表
	public String roleList(){
		String userName = (String)getRequest().getSession().getAttribute("userName");
		if(userName==null||"".equals(userName)){
			return "login";
		}
		List roleList  = loginService.roleList();
		getRequest().setAttribute("roleList", roleList);
		return SUCCESS;
	}
//	获得角色对应的权限列表
	public String rolePrivList(){
		String userName = (String)getRequest().getSession().getAttribute("userName");
		if(userName==null||"".equals(userName)){
			return "login";
		}
		RoleInfo roleInfo = new RoleInfo();
		roleInfo.setId(roleId);
		List rolePrivList = loginService.rolePrivList(roleInfo);
	
		ServletActionContext.getRequest().setAttribute("rolePrivList", rolePrivList);
		ServletActionContext.getRequest().setAttribute("roleId", roleId);
		return SUCCESS;
	}
//	提交更新的角色权限列表
	public String roleModify(){
		String str = loginService.roleModify(checkbox,roleId);
		return SUCCESS;
	}
//	找回密码
	public String findPassWord(){
		if("".equals(userName)||"".equals(mail)){
			return ERROR;
		}
//		产生随机密码
		Random r = new Random();
		passWord = Integer.toString(r.nextInt());
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName.trim());
		userInfo.setMail(mail);
		userInfo.setPassWord(Encrypt.MD5(passWord.trim()));
		String str = loginService.reSetPass(userInfo);
		if("error".equals(str)){
			return "error";
		}
//		如果成功了就发送邮件
		String mailbody = "您的新密码为："+passWord+",请在及时登录系统并修改您的密码，该邮件为系统自动发出，请勿回复此邮件，学报管理系统开发团队敬上。";
		SendMail themail = new SendMail("smtp.126.com");
		themail.setNeedAuth(true);
		if (themail.setSubject("学报管理系统，密码找回") == false){
			return ERROR; 
		}
		if (themail.setBody(mailbody) == false)
			return ERROR; 
		if (themail.setTo(mail) == false)
			return ERROR; 
		if (themail.setFrom("cheney_life@126.com") == false)
			return ERROR; 
		themail.setNamePass("cheney_life@126.com", "cheney");
		if (themail.sendout() == false)
			return ERROR; 
		getRequest().setAttribute("mail",mail);
		return SUCCESS;
	}
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public void setLoginService(ILoginService loginService) {
		this.loginService = loginService;
	}

	public void setApplyService(IApplyService applyService) {
		this.applyService = applyService;
	}

	public String getNewPassWord() {
		return newPassWord;
	}

	public void setNewPassWord(String newPassWord) {
		this.newPassWord = newPassWord;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public int[] getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(int[] checkbox) {
		this.checkbox = checkbox;
	}
}
